<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %>
<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %>
<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd">

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
        <script type="text/javascript" src="/nodes/frame/history.js-master/scripts/bundled/html4+html5/native.history.js"></script>       
        <script language="JavaScript" type="text/javascript">
            var initialized = false;
            var pushingState = false;
            var fetchingPage = false;
            var base = null;
            
            function init() {
                initialized = true;
                
                // Prepare history.js
                History = window.History;
                if ( !History.enabled ) {
                    return false;
                }
                
                History.debug.enable = false;
                
                // Bind to StateChange Even
                History.Adapter.bind(window,'statechange',function(){ // Note: We are using statechange instead of popstate
                    var State = History.getState(); // Note: We are using History.getState() instead of event.state
                    History.debug("statechange", State.data, State.title, State.url);
                    if (!pushingState) {
                        History.debug("fetch page");
                        fetchingPage = true;
                        contentFrame.location.href = State.url;
                    }
                    pushingState = false;
                });
                
                base = baseUrl(contentFrame.location);
        
                notify();
            }
            
            function notify() {
                if (!initialized)
                    return;
                
                var contentToolbar = contentFrame.document.getElementById("toolbar");
                var toolbar = toolbarFrame.document.getElementById("toolbar");
                
                if (toolbar != null && contentToolbar != null)
                    toolbar.innerHTML = contentToolbar.innerHTML;
                
                var contentActionBar = contentFrame.document.getElementById("actionBar");
                var actionBar = navigationFrame.document.getElementById("actionBar");
                
                if (actionBar != null && contentActionBar != null)
                    actionBar.innerHTML = contentActionBar.innerHTML;
                
                var contentTitle = contentFrame.document.title;
                top.document.title = contentTitle;
                
                updateNavigationFrame();
                navigationFrame.resizeApplet();
                var navigationApplet = navigationFrame.document.getElementById("navigationApplet");
                if (navigationApplet != null)
                    navigationApplet.update();
                
                var path = contentFrame.document.getElementById("navigation:repository:path");
                var fullPath = base + path.value;
                pushingState = true;

                if (History.emulated.pushState) {
                    if (!fetchingPage) {
                        History.debug("pushState:",{}, path.value, fullPath);                        
                        History.pushState({}, path.value, fullPath);
                    } 
                } else {
                    History.debug("replaceState:",{}, path.value, fullPath);                    
                    History.replaceState({}, path.value, fullPath);     
                }
                
                fetchingPage = false;
            }
            
            
            var originalCols = "280,*";
            
            function toggleNavigationFrame() {
                var showActions = contentFrame.document.getElementById("navigation:repository:showActions");
                
                if (showActions.value == "true") {
                    showActions.value = "false";
                } else {
                    showActions.value = "true";
                }
                
                updateNavigationFrame();
                
                var form = contentFrame.document.getElementById("navigation:repository");
                form.submit();
            }             
            
           function updateNavigationFrame() {
                var showActions = contentFrame.document.getElementById("navigation:repository:showActions");
                var navigationFrameset = parent.document.getElementById("navigation");
                
                var currentCols = navigationFrameset.cols;
                if (showActions.value == "false" && currentCols.charAt(0) != '0') {
                    originalCols = navigationFrameset.cols;
                    navigationFrameset.cols = "0,*";
                } else if (showActions.value == "true" && currentCols.charAt(0) == '0') {
                    navigationFrameset.cols = originalCols;
                }
            }             
            
            function baseUrl(url) {
                var pathComponents = url.pathname.split("/");
                var result =  url.protocol + "//" + url.host + "/";
                if (pathComponents.length >= 3)
                    result += pathComponents[1] + "/" + pathComponents[2];
                return result;
            }
            
            
        </script>
    </head>

    <f:view>
        <h:outputText escape="false" value='<frameset rows="140,*,30" frameborder="1" border="1" bordercolor="white" resize="yes" onload="init()">'/>
        <h:outputText escape="false" value='<frame name="toolbarFrame" src="#{NodesBean.repositoryPath}?contentType=frame&frame=toolbar" frameborder="0" resize="no" scrolling="no"/>'/>
        <h:outputText escape="false" value='<frameset id="navigation" cols="#{LayoutBean.showingActions ? \'280,*\' : \'0,*\'}" resize="yes">'/>
            <h:outputText escape="false" value='<frame name="navigationFrame" src="#{NodesBean.repositoryPath}?contentType=frame&frame=actionBar" 
                          frameborder="#{LayoutBean.noresizeIfNoBorder ? 1 : 0}" border="10" resize="yes" scrolling="no" framebordercolor="000000"/>'/>
            <h:outputText escape="false" value='<frame name="contentFrame" src="#{NodesBean.repositoryPath}#{PathBean.path}" onload="notify()" frameborder="0" resize="yes"/>'/>
        </frameset>
        <h:outputText escape="false" value='<frame name="footerFrame" src="#{NodesBean.repositoryPath}?contentType=frame&frame=footer" frameborder="0" resize="no" scrolling="no"/>'/>
    </frameset>
</f:view>    
